package Model;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Information.News;

public class NewsDAO{
	
	private String NHeadline;
	private String NContent;
	private String NImage;
	private String NSource;
	private Date PublishTime;
	private int TotalClick;
	
	public NewsDAO()
	{
		this.NHeadline="";
		this.NContent="";
		this.NImage="";
		this.NSource="";
		this.PublishTime = new Date(0);
		this.TotalClick=0;
	}
	
	public boolean AddNewNews(String NHeadline,String NContent,String NImage,String NSource) throws SQLException, ClassNotFoundException
	{
		String sql ="insert into News (NHeadline,NContent,NImage,NSource) values ('"+NHeadline+"','"+NContent+"','"+NImage+"','"+NSource+"')";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public ArrayList<News> SeeNews(String NHeadline) throws SQLException, ClassNotFoundException
	{
		ArrayList<News> arrayList = null;
		News News = new News();
		String sql ="select * from News where NHeadline ='"+NHeadline+"'";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		NHeadline = result.getString("NHeadline");
		NContent = result.getString("NContent");
		NImage = result.getString("NImage");
		NSource = result.getString("NSource");
		PublishTime = result.getDate("PublishTime");
		TotalClick = result.getInt("TotalClick");
		News.SetNewsInfo(NHeadline, NContent, NImage, NSource, PublishTime, TotalClick);
		arrayList.add(News);
		return arrayList;
	}
	
	public ArrayList<News> SeeAllNews() throws SQLException, ClassNotFoundException
	{
		ArrayList<News> arrayList = new ArrayList<News>();
		String sql ="select * from News";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		while(result.next())
		{
			News news = new News();
			NHeadline = result.getString("NHeadline");
			NContent = result.getString("NContent");
			NImage = result.getString("NImage");
			NSource = result.getString("NSource");
			PublishTime = result.getDate("PublishTime");
			TotalClick = result.getInt("TotalClick");
			news.SetNewsInfo(NHeadline, NContent, NImage, NSource, PublishTime, TotalClick);
			arrayList.add(news);
		}
		return arrayList;
	}
	
	public ArrayList<News> SeeNewsByKeyword(String Keyword) throws SQLException, ClassNotFoundException
	{
		ArrayList<News> arrayList = new ArrayList<News>();
		String sql ="select * from News where NContent like '"+"%"+Keyword+"%"+"' or NHeadline like '"+"%"+Keyword+"%"+"'";
		System.out.println(sql);
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		while(result.next())
		{
			News news = new News();
			NHeadline = result.getString("NHeadline");
			NContent = result.getString("NContent");
			NImage = result.getString("NImage");
			NSource = result.getString("NSource");
			PublishTime = result.getDate("PublishTime");
			TotalClick = result.getInt("TotalClick");
			news.SetNewsInfo(NHeadline, NContent, NImage, NSource, PublishTime, TotalClick);
			arrayList.add(news);
		}
		return arrayList;
	}
	
	
	public boolean DeleteNews(String NHeadline) throws ClassNotFoundException, SQLException
	{
		String sql ="delete from News where NHeadline='"+NHeadline+"'";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean UpdateNews(String NHeadline,String NContent,String NImage,String NSource) throws ClassNotFoundException, SQLException
	{
		//can not update EUser
		String sql ="update News set NHeadline='"+NHeadline+"',set NContent='"+NContent+"',set NImage='"+NImage+"',set NSource='"+NSource+"'where NHeadline='"+NHeadline+"'";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;//update successfully
		}
		else
		{
			return false;//update failed
		}
	}
}